package com.situ.mvc.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.websocket.server.PathParam;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.situ.mvc.entiry.Student;
import com.situ.mvc.entiry.StudentTeacher;
import com.situ.mvc.entiry.Teacher;
import com.situ.mvc.util.JSONResult;

// /student/insert.action
//@RequestMapping(value = "/student")
@Controller
@RequestMapping("/student")
public class StudentController {
	
	@RequestMapping("/insert1")
	public void insert1(String name, Integer age, String gender) {
		System.out.println("StudentController.insert1()");
		System.out.println("name: " + name);
		System.out.println("age: " + age);
		System.out.println("gender: " + gender);
	}

	@RequestMapping("/insert2")
	public ModelAndView insert2(Student student) {
		System.out.println("StudentController.insert1()");
		System.out.println(student);
		
		// request.setAttribute("student", student);
		// request.getRequestDispatcher("/student_info.jsp").forward();
		
		ModelAndView modelAndView = new ModelAndView();
		// 把数据放到域对象中
		modelAndView.addObject("student", student);
		// 设置要转发的页面
		modelAndView.setViewName("/student_info.jsp");
		return modelAndView;
	}
	
	// 默认情况下既可以支持get请求也可以支持post请求 
	// 如果只希望这个方法只能处理post请求method = RequestMethod.POST
	// @RequestMapping(value = "/insert", method = RequestMethod.POST)
	@RequestMapping(value = "/insert3", method = { /* RequestMethod.GET, */RequestMethod.POST})
	public String insert3(Student student, Model model) {
		System.out.println("StudentController.insert1()");
		System.out.println(student);
		
		// request.setAttribute("student", student);
		// request.getRequestDispatcher("/student_info.jsp").forward();
		
		model.addAttribute("student", student);
		return "/student_info.jsp"; // 转发到这个界面
	}
	
	// 使用原生的Servlet中的request、response、session
	@RequestMapping(value = "/insert")
	public void insert(HttpServletRequest request,HttpServletResponse response, HttpSession session) throws ServletException, IOException {
		System.out.println("StudentController.insert()");
		String name = request.getParameter("name");
		String age = request.getParameter("age");
		String gender = request.getParameter("gender");
		Student student = new Student(name, Integer.parseInt(age), gender);
		request.setAttribute("student", student);
		session.setAttribute("student", student);
		request.getRequestDispatcher("/student_info.jsp").forward(request, response);
	}
	
	@RequestMapping("/insertStudentTeacher")
	public String insertStudentTeacher(StudentTeacher studentTeacher) {
		System.out.println(studentTeacher);
		
		return "/student_info.jsp";
	}
	
	@RequestMapping("/deleteAll")
	public void deleteAll(HttpServletRequest request, Integer[] ids) {
		String[] idArray = request.getParameterValues("ids");
		for (String id : idArray) {
			System.out.println(id);
		}
		System.out.println(Arrays.toString(ids));
	}
	
	@RequestMapping("/selectAll")
	public void selectAll() {
		System.out.println("StudentController.selectAll()");
	}
	
	@RequestMapping("/deleteById")
	public String deleteById(Integer id) {
		System.out.println("id: " + id);
		
		// 转发到另一个jsp页面
		// return "/student_info.jsp";
		// 转发到Controller中的一个action请求
		// return "forward:/student/selectAll.action";
		// 重定向
		return "redirect:/student/selectAll.action";
	}
	
	
	// JSON格式转换
	@ResponseBody
	@RequestMapping("/selectList")
	public List<Student> selectList() {
		List<Student> list = new ArrayList<Student>();
		Student student1 = new Student(1, "zhansgan1", 23, "男");
		Student student2 = new Student(2, "zhansgan2", 23, "男");
		Student student3 = new Student(3, "zhansgan3", 23, "男");
		Student student4 = new Student(4, "zhansgan4", 23, "男");
		list.add(student1);
		list.add(student2);
		list.add(student3);
		list.add(student4);
		
		return list;
	}
	
	@ResponseBody
	@RequestMapping("/selectById")
	public Student selectById(Integer id) {
		Student student = new Student(1, "zhansgan1", 23, "男");
		return student;
	}
	
	@ResponseBody
	@RequestMapping("/selectJSONReulst")
	public JSONResult selectJSONReulst() {
		System.out.println("StudentController.selectJSONReulst()");
		List<Student> list = new ArrayList<Student>();
		Student student1 = new Student(1, "zhansgan1", 23, "男");
		Student student2 = new Student(2, "zhansgan2", 23, "男");
		Student student3 = new Student(3, "zhansgan3", 23, "男");
		Student student4 = new Student(4, "zhansgan4", 23, "男");
		list.add(student1);
		list.add(student2);
		list.add(student3);
		list.add(student4);
		
		JSONResult jsonResult = new JSONResult(JSONResult.ERROR, list);
		return jsonResult;
	}
	
	// @RequestParam
	// /Java2007SpringMVC/student/selectByPage.action?page=3&pageSize=34
	@RequestMapping("/selectByPage")
	public void selectByPage(@RequestParam(value = "page", defaultValue = "1") Integer pageNo, @RequestParam(required = true) Integer pageSize) {
		System.out.println("pageNo: " + pageNo);
		System.out.println("pageSize: " + pageSize);
	}
	
	// 视图解析器
	@RequestMapping("/getStudentInsertPage")
	public String getStudentInsertPage() {
		
		// return "/jsp/student_insert.jsp";
		return "student_insert";
	}
	
}
